<?php
/**
 *
 * Author:苏光盼
 * Date:2020/6/22 下午10:36
 * Class:Group.php
 * Web:https://app.admin.suanzisu.com
 * Project:通用APP后台管理系统
 *
 */

namespace App\HttpController\Admin;


use App\Model\AdmGroup;
use App\Logic\AdmGroup as AdmGroupLogic;
use App\Logic\RoleMenu as RoleMenuLogic;
use App\Model\GroupMenuMap;
use App\Model\GroupPriMap;
use App\Util\Common;
use EasySwoole\Component\Csp;
use EasySwoole\Http\Exception\Exception as HttpErr;
use EasySwoole\ORM\Exception\Exception;

class Group extends Base
{

    /**
     * 后台用户组
     */
    public function admin()
    {
        $data = Common::adv_list($this->request(),AdmGroup::class,null);
        return $this->lay(...$data);
    }



    public function admin_add(){
        $id = Common::check($this->request());
        $data = AdmGroupLogic::create()->get($id);
        $this->success($data ? $data->toArray() : []);
    }

    /**
     * 保存修改或新增的用户组
     */
    public function admin_save()
    {
        $data = $this->request()->getParsedBody('data');
        $id = AdmGroupLogic::update($data);
        return $this->success(['insert_id' =>$id]);
    }

    /**
     * @return mixed
     * 删除一个用户组
     * @throws Exception
     * @throws \Throwable
     */
    public function admin_del()
    {
        $auto_id = Common::check($this->request());
        AdmGroupLogic::del($auto_id);
        $this->success();
    }

    /**
     * 为后台用户组增权限
     */
    public function admin_add_pri()
    {
        $ret = AdmGroupLogic::actPri($this->request(),'add');
        if( $ret > 0 ) return $this->success();
        throw new HttpErr('the id must be has!',403);
    }

    /**
     * 删除后台用户组的某些权限
     */
    public function admin_del_pri()
    {
        $map_auto_id = $this->request()->getParsedBody('ids');
        if(!$map_auto_id)  throw new HttpErr('the ids must be has!',403);
        $ret = GroupPriMap::create()->destroy($map_auto_id);
        if( $ret > 0 )
            return $this->success();
        throw new HttpErr('the id must be has', 403);
    }


    /**
     * 列出后台用户组当前的权限菜单
     */
    public function admin_role_menu()
    {
        $group_id =   Common::check($this->request(),'group_id');
        $chan = new Csp();
        $data =  $chan->add('all_role_menu', function () {
            return RoleMenuLogic::findByTree();
        })->add('group_role_menu_ids',function () use($group_id){
            $group_role_menu_ids = AdmGroupLogic::findRoleMenuIdByGroupId($group_id);
            $all_pids = [];
            if($group_role_menu_ids){
                $all_pids = \App\Model\RoleMenu::create()->where('pid',$group_role_menu_ids,'IN')->column('pid');
            }
            if($all_pids){
                return   array_values( array_diff(
                    $group_role_menu_ids, array_unique(
                    array_filter( $all_pids ) ) ) );
            }
            return null;

        })->exec();
        return $this->success($data);
    }


    /**
     * @return mixed
     * @throws Exception
     * @throws HttpErr
     * @throws \EasySwoole\Mysqli\Exception\Exception
     * 保存用户组权限菜单的修改
     * @throws \Throwable
     */
    public function admin_role_menu_save()
    {
        $ids = $this->request()->getParsedBody('ids');
        $group_id = $this->request()->getParsedBody('group_id');
        if( !$group_id || !$ids) throw new HttpErr('the group_id or Ids must be has', 403);
        $is_has_group_id = AdmGroup::create()->get($group_id);
        if( !$is_has_group_id ) throw new HttpErr( 'the group data is empty', 403);
        $ret = AdmGroupLogic::saveGroupMenuMap((array)$ids,$group_id);
        if( $ret > 0 ){
            return $this->success();
        }
        throw new HttpErr('save group menu map is fail', 403);
    }

    /**
     *
     * 用户的菜单排序修改
     */
    public function admin_role_menu_ord(){
        $data = $this->request()->getParsedBody();
        $groupMapData = GroupMenuMap::create()->get($data['id']);
        if(!$groupMapData) throw new HttpErr('参数错误！ID不存在！',403);
        $groupMapData->update($data);
        $this->success();
    }
}